package com.wxmit.starter.xfile.core;

import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageProperties;
import org.dromara.x.file.storage.core.recorder.FileRecorder;
import org.dromara.x.file.storage.core.upload.FilePartInfo;
import org.dromara.x.file.storage.spring.SpringFileStorageProperties;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 *  读取本地文件的 recorder
 * @author huangxiong
 * @date 2024/04/10 20:55
 */
public class SysFileRecorder implements FileRecorder {


    private final SpringFileStorageProperties properties;

    // 默认是本地配置文件
    private FileStorageProperties.LocalPlusConfig fileConfig;

    @Value("${ss.file.domain}")
    private String dfDomain;

    public SysFileRecorder(SpringFileStorageProperties properties) {
        this.properties = properties;
        init();
    }

    public void init(){
        FileStorageProperties fileStorageProperties = properties.toFileStorageProperties();
        fileConfig = fileStorageProperties.getLocalPlus().get(0);
    }

    @Override
    public boolean save(FileInfo fileInfo) {
        return true;
    }

    @Override
    public void update(FileInfo fileInfo) {
        // 主要是分片用到
    }

    @Override
    public FileInfo getByUrl(String url) {
        if(Objects.isNull(url)){
            return null;
        }
        FileInfo fileInfo = new FileInfo();
        fileInfo.setPlatform(properties.getDefaultPlatform());
        if(url.startsWith(dfDomain)){
            fileInfo.setUrl(url);
            fileInfo.setPath(url.replace(dfDomain, ""));
        }else{
            fileInfo.setUrl(dfDomain + url);
            fileInfo.setPath(url);
        }
        return fileInfo;
    }

    @Override
    public boolean delete(String url) {
        return true;
    }

    @Override
    public void saveFilePart(FilePartInfo filePartInfo) {

    }

    @Override
    public void deleteFilePartByUploadId(String s) {

    }
}
